package com.foreverwin.jzymes.websocket;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.foreverwin.jzymes.common.enums.TaskStatusEnum;
import com.foreverwin.jzymes.common.enums.TaskTypeEnum;
import com.foreverwin.jzymes.common.mapper.DashBoardAdviceMapper;
import com.foreverwin.jzymes.common.service.IActivityRuleAdviceService;
import com.foreverwin.jzymes.common.service.IProductionShiftService;
import com.foreverwin.jzymes.common.service.IResourceAdviceService;
import com.foreverwin.jzymes.framework.annotation.WebSocketHandler;
import com.foreverwin.jzymes.framework.event.AppEvent;
import com.foreverwin.jzymes.framework.util.DateUtil;
import com.foreverwin.jzymes.framework.websocket.TextMessageWebSocketWraper;
import com.foreverwin.jzymes.qc.check.domain.QcCheck;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Map;

@WebSocketHandler( channel = "Z_DB_PROCESS_INSP" )
public class ProcessInspectHandler extends TextMessageWebSocketWraper {

    @Autowired
    IActivityRuleAdviceService activityRuleAdviceService;
    @Autowired
    IResourceAdviceService resourceAdviceService;
    @Autowired
    IProductionShiftService productionShiftService;
    @Autowired
    DashBoardAdviceMapper dashBoardAdviceMapper;

    private Map<String,String> activityRuleMap;

    @Override
    public void doAfterOpen(Connection connection) {
        activityRuleMap = activityRuleAdviceService.findActivityRules( getChannel() );
    }

    @Override
    public void doAfterClose() {

    }

    @Override
    public JSONObject doReceiveAndReply(String messageType, JSONObject requestParams) {
        if( "WS_INIT_DATA".equals( messageType ) ){
            buildProcessInpectData();
        }
        return null;
    }

    @Override
    public void onAppEvent(AppEvent<?> appEvent) {

    }

    public void buildProcessInpectData(){
        String site = activityRuleMap.get( "SITE" );

        JSONArray details = new JSONArray();
        JSONObject workCenters = new JSONObject();
        Long totalInspectQty = 0L;
        Long totalNcQty = 0L;

        String shift = productionShiftService.findShift( site, new Date() );
        Date[] dateRange = productionShiftService.findShiftRange( site, shift );

        int section = findSection( dateRange );

        List<Map<String,Object>> cncList = resourceAdviceService.findResourceListByCncType( site, null, "CNC" );
        for( Map<String,Object> cncMap : cncList ){
            String resource = (String) cncMap.get( "RESRCE" );
            String workCenter = resourceAdviceService.findWorkCenter( site, resource );
            workCenter = activityRuleMap.get( workCenter );

            JSONObject detail = new JSONObject();
            detail.put( "id", resource );
            detail.put( "checkQty", 0L );
            detail.put( "ncQty", 0L );
            details.add( detail );

            JSONObject workCenterDetail = null;
            if( workCenters.containsKey( workCenter + "_Area" ) ){
                workCenterDetail = workCenters.getJSONObject( workCenter + "_Area" );
            }else{
                workCenterDetail = new JSONObject();
                workCenterDetail.put( "id", workCenter + "_Area" );
                workCenterDetail.put( "checkQty", 0L );
                workCenterDetail.put( "ncQty", 0L );
                workCenters.put( workCenter, workCenterDetail );
                details.add( workCenterDetail );
            }

            List<QcCheck> qcCheckList = dashBoardAdviceMapper.findQcCheck( site, resource, TaskTypeEnum.IPQC.name(), TaskStatusEnum.CLOSED.value(), dateRange[0], dateRange[1]  );
            for( QcCheck qcCheck : qcCheckList ){
                if( !"Y".equals( qcCheck.getCheckResult() ) ){
                    detail.put( "ncQty", detail.getLong( "ncQty" ) + 1 );
                    workCenterDetail.put( "ncQty", workCenterDetail.getLong( "ncQty" ) + 1 );
                    totalNcQty = totalNcQty + 1;
                }
                detail.put( "checkQty", detail.getLong( "checkQty" ) + 1 );
                workCenterDetail.put( "checkQty", workCenterDetail.getLong( "checkQty" ) + 1 );
                totalInspectQty = totalInspectQty + 1;
            }
        }

        JSONObject response = new JSONObject();
        response.put( "inspQty", totalInspectQty );
        response.put( "ncQty", totalNcQty );
        response.put( "shift", shift );
        response.put( "section", section );
        response.put( "detail", details );
        sendMessage( response );
    }

    public int findSection( Date[] dateRange ){
        int hourInterval = Integer.valueOf( activityRuleMap.get( "HOUR_INTERVAL" ) );
        Date now = new Date();
        int section = 1;
        for( dateRange[1] = DateUtil.addHours( dateRange[0], hourInterval ); now.after( dateRange[1] );  ){
            section++;
            dateRange[0] = DateUtil.addHours( dateRange[0], hourInterval );
            dateRange[1] = DateUtil.addHours( dateRange[1], hourInterval );
        }
        return section;
    }
}
